package com.itheima.health.jobs;

import com.itheima.health.common.RedisConst;
import com.itheima.health.utils.QiniuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.Set;

/**
 * @program: itcast_health
 * @create: 2019-10-21 20:33
 * @author: li he
 * @description:
 */
public class ClearImgJob {


    @Autowired
    private JedisPool jedisPool;


    public void clearImg(){

        //获取redis 两个数据库中的差值
        Set<String> sdiff = jedisPool.getResource().sdiff(RedisConst.SETMEAL_PIC_RESOURCES, RedisConst.SETMEAL_PIC_DB_RESOURCES);

        System.out.println("ClearImgJob.clearImg===== 开始清理垃圾图片"+new Date());
        if (sdiff.size() >0 ){
            for (String img : sdiff) {
                //删除七牛云上的垃圾图片
                QiniuUtil.deleteFileFromQiniu(img);
                //删除redis中的垃圾图片的名称
                jedisPool.getResource().srem(RedisConst.SETMEAL_PIC_RESOURCES,img);
                System.out.println("图片名称 : " + img);
            }
        }
    }
}